<template>
  <h1>员工列表练习</h1>
  <el-form style="width: 600px;margin: 0 auto;" label-width="80px">
    <el-form-item label="姓名">
      <el-input placeholder="请输入员工姓名" v-model="emp.name" style="width: 240px"></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input placeholder="请输入员工工资" v-model="emp.salary" style="width: 240px"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input placeholder="请输入员工岗位" v-model="emp.job" style="width: 240px"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success"  @click="addEmp">点我添加</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="arr" style="width: 600px;margin: 0 auto;">
    <el-table-column width="80px" type="index" label="编号"></el-table-column>
    <el-table-column prop="name" label="员工姓名"></el-table-column>
    <el-table-column prop="salary" label="员工工资"></el-table-column>
    <el-table-column prop="job" label="员工岗位"></el-table-column>
    <el-table-column  label="操作">
      <template #default="scope">
        <el-button type="danger" @click="delEmp(scope.row,scope.$index)">删除</el-button>
      </template>

    </el-table-column>
  </el-table>
</template>

<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";

//定义响应式对象用来保存收集到的员工数据
const emp = ref({name:'',salary:'',job:''});
//定义数组用来保存表格中的多个员工数据
const arr = ref([]);
//定义添加员工的方法
const addEmp = ()=>{
  //判断是否有未输入的数据
  if(emp.value.name.trim()=='' || emp.value.salary.trim()=='' || emp.value.job.trim()==''){
    alert('请填写完整员工信息!');
    return;
  }
  //将收集到的员工数据存入数组中
  arr.value.push(emp.value);
  //注意:用完的数据一定要清空!
  emp.value = {name:'',salary:'',job:''};
  console.log(arr.value);
}
//定义删除员工方法
const delEmp = (e,index) => {
  if(confirm('您确认要删除'+e.name+'吗?')){
    arr.value.splice(index,1);
    ElMessage.success('删除成功!')
  }

}
</script>

<style scoped>

</style>